package org.jeecg.modules.crm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.crm.entity.CrmContact;
import org.jeecg.modules.crm.mapper.CrmContactMapper;
import org.jeecg.modules.crm.service.ICrmContactService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description: 客户联系人
 * @Author: jeecg-boot
 * @Date:   2023-06-03
 * @Version: V1.0
 */
@Service
public class CrmContactServiceImpl extends ServiceImpl<CrmContactMapper, CrmContact> implements ICrmContactService {

    private static final Logger log = LoggerFactory.getLogger(CrmContactServiceImpl.class);

    @Override
    public List<CrmContact> getContactsByCustomerId(String customerId) {
        return baseMapper.selectByCustomerId(customerId);
    }

    @Override
    public CrmContact getPrimaryContactByCustomerId(String customerId) {
        return baseMapper.selectPrimaryContact(customerId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean setPrimaryContact(String id, String customerId) {
        // 先将所有联系人设置为非主要联系人
        baseMapper.resetPrimaryContact(customerId);
        
        // 再将指定联系人设置为主要联系人
        CrmContact contact = new CrmContact();
        contact.setId(id);
        contact.setIsPrimary(1);
        return this.updateById(contact);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByCustomerId(String customerId) {
        return baseMapper.deleteByCustomerId(customerId) >= 0;
    }

    @Override
    public List<Map<String, Object>> getSuggestContactList(String keyword, Integer limit) {
        if (keyword == null || keyword.trim().isEmpty()) {
            return new ArrayList<>();
        }
        
        keyword = keyword.trim();
        log.info("执行联系人智能提示查询，关键字: {}", keyword);
        
        // 构建查询条件
        QueryWrapper<CrmContact> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("contact_name", keyword)
                   .or().like("phone", keyword)
                   .or().like("email", keyword)
                   .orderByDesc("create_time")
                   .last("limit " + limit);
        
        List<CrmContact> contacts = baseMapper.selectList(queryWrapper);
        
        // 转换为Map列表，便于前端使用
        List<Map<String, Object>> result = new ArrayList<>();
        for (CrmContact contact : contacts) {
            Map<String, Object> item = new HashMap<>();
            item.put("id", contact.getId());
            item.put("contactName", contact.getContactName());
            item.put("phone", contact.getPhone());
            item.put("email", contact.getEmail());
            item.put("position", contact.getPosition());
            item.put("customerId", contact.getCustomerId());
            result.add(item);
        }
        
        return result;
    }
} 